var devices = null;
var device = null;

function checkBlueTooth() {
    try {
        Bluetooth.checkBluetooth(function (success) {
            alert(success)
        }, function (error) {
            alert(error)
        })
    } catch (error) {
        alert(error);
    }
}

function openBlueTooth() {
    try {
        Bluetooth.openBluetooth(function (success) {
            alert(success)
        }, function (error) {
            alert(error)
        })
    } catch (error) {
        alert(error);
    }
}

function getDevices() {
    $(".bluetooth").show();
    $(".mengceng").show();
    $(".bluetooth .ly_content").html("");
    if (devices == null) {
        searchDevices();
    } else {
        listDevices();
    }
}

function listDevices() {
    if (devices.length == 0) {
        $(".bluetooth .ly_content").html("<p>没有蓝牙设备,请重新扫描</p>");
    } else {
        var ly_content = "";
        for (var i = 0; i < devices.length; i++) {
            var device = devices[i].split("\n");
            var name = device[0];
            var address = device[1];
            ly_content += "<p data='" + name + "\n" + address + "'>" + name + " (" + address + ")</p>"
        }
        $(".bluetooth .ly_content").html(ly_content);
        $(".bluetooth").show();
        $(".mengceng").show();
    }
}

function searchDevices() {
    try {
        $(".bluetooth .ly_content").html("<p style='text-align:center;'>正在扫描蓝牙设备...</p>");
        Bluetooth.searchDevices(function (success) {
            if (success != "START") {
                saveDevices(success);
                devices = success.split("\n\n");
                if (devices.length == 0) {
                    $(".bluetooth .ly_content").html("<p>没有扫描到蓝牙设备</p>");
                } else {
                    var ly_content = "";
                    for (var i = 0; i < devices.length; i++) {
                        var device = devices[i].split("\n");
                        var name = device[0];
                        var address = device[1];
                        ly_content += "<p data='" + name + "\n" + address + "'>" + name + " (" + address + ")</p>"
                    }
                    $(".bluetooth .ly_content").html(ly_content);
                    $(".bluetooth").show();
                    $(".mengceng").show();
                }
            }
        }, function (error) {
            $(".bluetooth .ly_content").html(error);
        })
    } catch (error) {
        $(".bluetooth .ly_content").html(error);
    }
}

function stopSearchDevices() {
    try {
        Bluetooth.stopSearchDevices(function (success) {
        }, function (error) {
        })
    } catch (error) {
    }
}

function saveDevice(device) {
    try {
        Bluetooth.saveDevice(function (success) {
        }, function (error) {
        }, device)
    } catch (error) {
    }
}

function connectDevice(device) {
    try {
        Bluetooth.connectDevice(function (success) {
        }, function (error) {
            alert(error)
        }, device)
    } catch (error) {
    }
}

function saveDevices(devices) {
    try {
        Bluetooth.saveDevices(function (success) {
        }, function (error) {
        }, devices)
    } catch (error) {
    }
}

function getSaved() {
    getSavedDevice();
    getSavedDevices();
}

function getSavedDevice() {
    try {
        Bluetooth.getSavedDevice(function (success) {
            if (success) device = success;
            var name = device.split("\n")[0];
            // $("#getBluetoothWeightButton").text("蓝牙获取重量(" + name + ")");
            $("#getBluetoothWeightButton").text("获取重量");
        }, function (error) {
        })
    } catch (error) {
    }
}

function getSavedDevices() {
    try {
        Bluetooth.getSavedDevices(function (success) {
            if (success) devices = success.split("\n\n");
        }, function (error) {
        });
    } catch (error) {
    }
}

//纳威克的蓝牙吊秤的取数方式
// function getData(address) {
//     try {
//         Bluetooth.getData(function (success) {
//             if (success != "START") {
//                 $alert("成功获取：" + success);
//                 var array = success.includes("+") ? success.split("+") : success.includes("-") ? success.split("-") : [];
//                 if (array.length == 0) {
//                     $alert("未获取到蓝牙重量");
//                 } else {
//                     var weight = array[array.length - 1];
//                     weight = weight.replace('k', '')
//                     weight = weight.replace('g', '')
//                     weight = weight.replace('N', '')
//                     weight = Number(weight.replace('K', ''))
//                     weight = Number(parseFloat(weight / 1000).toFixed(3));
//                     // alert(weight);
//                     // 复称入库 页面  的 毛重 框
//                     $("input.cpmz").val(weight);
//                     // 钢丝绳称重 页面 的 毛重 框
//                     $("input.mz").val(weight);
//                     weight = parseInt(weight);
//                     $("div.div_pt input.cpzl").val(weight);
//                     $("div.div_shouxian input.cpmz").val(weight);
//                 }
//             } else {
//                 // $alert("=START");
//             }
//         }, function (error) {
//             alert(error)
//         }, address);
//     } catch (error) {
//     }
// }

//澳森一期/二期新买的北京的蓝牙吊秤的取数方式
function getData(address) {
    try {
        Bluetooth.getData(function (success) {
            if (success != "START") {
                $alert("成功获取：" + success);
                var weight = Number(success);
                weight = Number(parseFloat(weight / 1000).toFixed(3));
                // 复称入库 页面  的 毛重 框
                $("input.cpmz").val(weight).trigger("change");
                // 钢丝绳称重 页面 的 毛重 框
                $("input.mz").val(weight).trigger("change");
                weight = parseInt(weight);
                $("div.div_pt input.cpzl").val(weight);
                $("div.div_shouxian input.cpmz").val(weight);

            } else {
                // $alert("=START");
            }
        }, function (error) {
            alert(error)
        }, address);
    } catch (error) {
    }
}


$(document).on('click', '#cancelBluetooth', function () {
    $(".bluetooth").hide();
    $(".mengceng").hide();
    $(".bluetooth .ly_content").html("");
    stopSearchDevices();
})

$(document).on('click', '#searchBluetooth', function () {
    $(".bluetooth .ly_content").html("");
    searchDevices();
})

$(document).on('click', '#getBluetoothWeightButton', function () {
    // if (device == null) {
    //     $alert("请选择蓝牙");
    // } else {
    //     getData(device.split("\n")[1]);
    // }
    getData();
})

$(document).on('click', '.bluetooth .ly_content p', function (event) {
    device = $(event.target).attr('data');
    var name = device.split("\n")[0];
    saveDevice(device);
    connectDevice(device.split("\n")[1]);
    $alert("蓝牙连接成功");
    $(".bluetooth").hide();
    $(".mengceng").hide();
    // $("#getBluetoothWeightButton").text("蓝牙获取重量(" + name + ")");
    $("#getBluetoothWeightButton").text("获取重量");
});

$(document).on('click', '#selectBluetooth', function (event) {
    Bluetooth.checkBluetooth(function (success) {
        Bluetooth.openBluetooth(function (success) {
            getDevices();
        }, function (error) {
            $alert(error)
        })
    }, function (error) {
        $alert(error)
    })
});

document.addEventListener('deviceready', this.getSaved, false);
